from django.shortcuts import render,HttpResponse,redirect,HttpResponseRedirect,HttpResponsePermanentRedirect
from django.http import JsonResponse
from app01.models import Image,UserGoods,Userinfo
from myproject1 import settings
from django.views import View #cbv
from django.contrib import auth# 认证模块,用户
# from django.contrib.auth.models import User # 对应数据库
from django.contrib.auth import authenticate,get_user_model,login,logout
import os,json
from django.db.models import Avg,Max,Min,Count,Sum,Q,F  #   引入函数
from django.core import serializers
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth.decorators import login_required
# Create your views here.
User=get_user_model()
def test(request):
    own=request.user
    # thedel(request,own)
    return render(request,"test1.html")
@login_required
def index_web(request):
    own=request.user
    goods=UserGoods.objects.all()
    return render(request,"index_web.html",{"goods":goods,"user":own})
def register(request):
    if request.method=="GET":
        return render(request,"index_register.html")
    elif 'login' in request.POST:
        username_login=request.POST.get("snum")
        pwd_login=request.POST.get("pwd")
        user_login=authenticate(password=pwd_login,username=username_login)
        if user_login:
            login(request,user_login)
            return  HttpResponseRedirect('/index/')#各重定向，其他页面跳转回来直接转到index
        else:
            return render(request,"index_register.html",{'error_msg1':"登录失败，请重新输入"})
    else:
        username=request.POST.get('snum')
        pwd=request.POST.get('pwd')
        nicname=request.POST.get('name')
        if not User.objects.filter(username=username):
            User.objects.create_user(username=username,password=pwd,Name=nicname)
            user_regi=authenticate(password=pwd,username=username)
            login(request,user_regi)
            return  HttpResponseRedirect('/index/')
        else:
            return render(request,"index_register.html",{"error_msg0":"该id已注册"})
def vlogout(request):
    logout(request)
    return redirect('/register/')
@login_required
def goods(request):
    own=request.user
    go=UserGoods.objects.filter(username=own)
    return render(request,"ordertest.html",{"good":go,"user":own})
def addgoods(request):
    own=request.user
    good=UserGoods.objects.create(username=own)
    good.price=request.POST.get('price')
    good.countnum=request.POST.get('count')
    good.goodtype=request.POST.get('sellOrbuy')
    good.goodstatus=request.POST.get('visible')
    good.name=request.POST.get('item-name')
    good.describe=request.POST.get('description')
    file_img=request.FILES.get('item_upload')
    if file_img:
        good.image=file_img
    good.save()
    return  HttpResponseRedirect("/order/")
def addgood1(request):
    own=request.user
    the=request.POST.get('id')
    go=UserGoods.objects.get(id=the,username=own)
    go.countnum+=1
    go.save()
    print(go.countnum)
    return  HttpResponseRedirect("/order/")
def delgoods(request):
    # own=request.user
    # good=UserGoods.objects.filter(username=own) 
    # if request.method=="GET":
    #     return  HttpResponseRedirect("/order/",{"good":go,"user":own})
    own=request.user
    the=request.POST.get('id')
    go=UserGoods.objects.get(username=own,id=the)
    if go.image:
        thedel(request,go)
    go.delete()
    return HttpResponseRedirect('/order/')
def sold(request):
    own=request.user
    the=request.POST.get('id')
    go=UserGoods.objects.get(username=own,id=the)
    if go.countnum>=1:
        go.countnum-=1
    if go.countnum==0:
        go.goodstatus='invisible'
    go.save()
    return HttpResponseRedirect('/order/')
def changevis(request):
    own=request.user
    the=request.POST.get('id')
    go=UserGoods.objects.get(username=own,id=the)
    if go.goodstatus=='visible':
        go.goodstatus='invisible'
    else: go.goodstatus='visible'
    go.save()
    return HttpResponseRedirect('/order/')
def changegoods(request):
    own=request.user
    the=request.POST.get('id')
    good=UserGoods.objects.get(username=own,id=the)
    print(good)
    good.price=request.POST.get('price')
    good.countnum=request.POST.get('count')
    good.goodtype=request.POST.get('sellOrbuy')
    good.goodstatus=request.POST.get('visible')
    good.name=request.POST.get('item-name')
    good.describe=request.POST.get('description')
    file_img=request.FILES.get('item_upload')
    print(good.countnum)
    if file_img:
        thedel(request,good)
        good.image=file_img
    good.save()
    return  HttpResponseRedirect("/order/")
def orderedit(request):
    own=request.user
    the=request.POST.get('id')
    go=UserGoods.objects.filter(username=own)
    goo=UserGoods.objects.get(username=own,id=the)    
    return render(request,'ordertest.html',{"ed":goo,"type":"block","good":go})
@login_required
def pinfo(request):
    own=request.user
    if request.method=='GET':
        return render(request,"personal_info.html",{'media':own})
    avatr=request.FILES.get('file')
    if(own.Avatr!='null' and avatr ):
        thedel(request,own)
    if avatr:
        own.Avatr=avatr
    own.Name=request.POST.get('name')
    own.Gender=request.POST.get('gender')
    own.WX_id=request.POST.get('wxnumber')
    own.Phone=request.POST.get('phone')
    own.Introduction=request.POST.get('message')
    own.location=request.POST.get('school')
    own.save()
    return HttpResponseRedirect('/personalinfo/',{'media':own})
def thedel(request,item):
    if isinstance(item,Userinfo):
        img_file=item.Avatr
        if img_file!='null':
            path='app01/media/'+str(img_file)
            os.remove(path)
    else: 
        img_file=item.image
        if img_file!='null':    
            path='app01/media/'+str(img_file)
            print(path)
            os.remove(path)
def fcampus(request):
    own=request.user
    cap=Userinfo.objects.filter(location=1)
    goods=UserGoods.objects.none()
    for i in cap:
        goods2=UserGoods.objects.filter(username=i)
        goods=goods.union(goods2)
    return render(request,"index_web.html",{"goods":goods,"user":own})
def hcampus(request):
    own=request.user
    cap=Userinfo.objects.filter(location=2)
    goods=UserGoods.objects.none()
    for i in cap:
        goods2=UserGoods.objects.filter(username=i)
        goods=goods.union(goods2)
    return render(request,"index_web.html",{"goods":goods,"user":own})    

        

    